/*
 * Rectangle2D.cpp
 *
 *  Created on: 31.1.2013
 *      Author: Travis
 */

#ifdef _WIN32 
#include <windows.h> 
#endif

#include "Rectangle2D.h"

namespace DataTypes
{

Rectangle2D::Rectangle2D(glm::vec2 a, glm::vec2 b, glm::vec2 c, glm::vec2 d)
{
	A = a;
	B = b;
	C = c;
	D = d;
}

Rectangle2D::Rectangle2D()
{
}

Rectangle2D::~Rectangle2D()
{
}

bool Rectangle2D::pointInRectangle2D(glm::vec2 X)
{
	if ((X.x >= B.x) && (X.x <= D.x) && (X.y >= B.y) && (X.y <= D.y))
		return 1;

	return 0;
}

bool Rectangle2D::pointInRectangle2D(glm::vec2 X, glm::vec2 pos)
{
	if ((X.x >= (B.x + pos.x)) && (X.x <= (D.x + pos.x)) && (X.y >= (B.y + pos.y))
			&& (X.y <= (D.y + pos.y)))
		return 1;

	return 0;
}

bool Rectangle2D::pointInRectangle2D(float angle, glm::vec2 pos, glm::vec2 X)
{
	// create transformation matrix
	glm::mat4 ViewMatrix = glm::rotate(glm::mat4(1.0), angle,
			glm::vec3(0.0f, 1.0f, 0.0f));
	ViewMatrix = glm::translate(ViewMatrix, glm::vec3(pos.x, 0.0, pos.y));

	glm::mat4 rMatrix = glm::rotate(glm::mat4(1.0), angle,
			glm::vec3(0.0f, 1.0f, 0.0f));

	glm::vec4 movedD(D.x, 0.0, D.y, 1.0);
	glm::vec4 movedB(B.x, 0.0, B.y, 1.0);

	glm::vec4 movedX(X.x, 0.0, X.y, 1.0);

	movedD = ViewMatrix * movedD;
	movedB = ViewMatrix * movedB;
	movedX = rMatrix * movedX;

	if (((movedX.x >= movedB.x) && (movedX.x <= movedD.x))
			&& ((movedX.z >= movedB.z) && (movedX.z <= movedD.z)))
		return 1;

	return 0;
}

bool Rectangle2D::RectangleInRectangle2(Rectangle2D rec)
{
	if (C.y <= rec.A.y)
		return 0;

	if (A.y >= rec.C.y)
		return 0;

	if (D.x <= rec.A.x)
		return 0;

	if (A.x >= rec.D.x)
		return 0;

	return 1;
}

//get
glm::vec2 Rectangle2D::getA()
{
	return A;
}

glm::vec2 Rectangle2D::getB()
{
	return B;
}

glm::vec2 Rectangle2D::getC()
{
	return C;
}

glm::vec2 Rectangle2D::getD()
{
	return D;
}

//set
void Rectangle2D::setA(glm::vec2 a)
{
	A = a;
}

void Rectangle2D::setB(glm::vec2 b)
{
	B = b;
}

void Rectangle2D::setC(glm::vec2 c)
{
	C = c;
}

void Rectangle2D::setD(glm::vec2 d)
{
	D = d;
}
}

